package cn.wang.service.impl;

import cn.wang.common.Exceptions.impl.LyException;
import cn.wang.common.enums.impl.ExceptionEnum;
import cn.wang.mapper.ISpecGroupMapper;
import cn.wang.mapper.ISpecParamMapper;
import cn.wang.pojo.SpecGroup;
import cn.wang.pojo.SpecParam;
import cn.wang.service.ISpecificationService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SpecificationService implements ISpecificationService {

    @Autowired
    private ISpecGroupMapper specGroupMapper;

    @Autowired
    private ISpecParamMapper specParamMapper;
    /**
     * 根据商品分类查询,商品规格组
     * @param cid
     * @return
     */
    @Override
    public List<SpecGroup> queryGroupsByCid(Long cid) {
        if (cid!=null&&cid==0){
            throw new LyException(ExceptionEnum.CATEGORY_ID_IS_NULL);
        }
        SpecGroup group = new SpecGroup();
        group.setCid(cid);
        List<SpecGroup> groups = specGroupMapper.select(group);
        if (CollectionUtils.isEmpty(groups)){
            throw new LyException(ExceptionEnum.GROUP_NOT_FIND);
        }
        return groups;
    }

    /**
     * 保存商品规格分组
     * @param group
     * @return
     */
    @Override
    public int saveGroup(SpecGroup group) {
        return specGroupMapper.insert(group);
    }

    @Override
    public int UpdateGroup(SpecGroup group) {
        return specGroupMapper.updateByPrimaryKey(group);
    }

    @Override
    public int deleteGroupById(Long gid) {
        return specGroupMapper.deleteByPrimaryKey(gid);
    }

    @Override
    public List<SpecParam> queryPrams(Long gid, Long cid, Boolean searching) {
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        return specParamMapper.select(specParam);
    }

    @Override
    public List<SpecGroup> queryGroupsAllByCid(Long cid) {
        List<SpecGroup> specGroups = queryGroupsByCid(cid);
        // 查询所有cid下的规格参数
        List<SpecParam> specParams = queryPrams(null, cid, null);// 查询所有的规格参数, 不管是否用于搜索
        Map<Long,List<SpecParam>> map = new HashMap<>();
        // 将specParams转成以groupId为将, 所属paramList为值的map集合
        for (SpecParam specParam : specParams) {
            if (!map.containsKey(specParam.getGroupId())){
                map.put(specParam.getGroupId(),new ArrayList<>());
            }
            map.get(specParam.getGroupId()).add(specParam);
        }
        // 向SpecGroup中添加param列表
        for (SpecGroup specGroup : specGroups) {
            specGroup.setParams(map.get(specGroup.getId()));
        }
        return specGroups;
    }

    @Override
    public Integer saveParam(SpecParam specParam) {
        return specParamMapper.insert(specParam);
    }

    @Override
    public Integer deleteParamByPid(Long pid) {
        return specParamMapper.deleteByPrimaryKey(pid);
    }

    @Override
    public Integer updateParam(SpecParam specParam) {
        return specParamMapper.updateByPrimaryKey(specParam);
    }
}
